package visiopuce.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import visiopuce.ICommandIds;
import visiopuce.ui.RechercheRegionDialog;
import visiopuce.ui.view.GrapheView;

public class RechercheRegionPopupAction extends Action {

	private final IWorkbenchWindow window;

	public RechercheRegionPopupAction(String text, IWorkbenchWindow window) {
		super(text);
		this.window = window;
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_OUVRIR_RECHERCHER_REGION);
		// Associate the action with a pre-defined command, to allow key
		// bindings.
		setActionDefinitionId(ICommandIds.CMD_OUVRIR_RECHERCHER_REGION);
		setText("Rechercher une région dans le graphe des puces");
		setImageDescriptor(visiopuce.Activator.getImageDescriptor("/icons/graphe.png"));
	}

	public void run() {
		RechercheRegionDialog rechercheRegionDialog = new RechercheRegionDialog(window.getShell());
		if (rechercheRegionDialog.open() == IDialogConstants.OK_ID) {
			try {
				ViewPart viewp = (ViewPart) window.getActivePage().findView("visiopuce.grapheView");
				if (viewp != null) {
					viewp.dispose();
				}

				window.getActivePage().showView("visiopuce.grapheView");
				GrapheView view = (GrapheView) window.getActivePage().findView("visiopuce.grapheView");

			} catch (PartInitException e) {
				e.printStackTrace();
				MessageDialog.openError(window.getShell(), "Error", "Erreur lors de l'ouverture de la vue du graphe :" + e.getMessage());
			}
		}
	}
}